from pathlib import Path

path = Path("guest.txt")

guest_names = []

prompt = "\nHi, what's your name? "
prompt += "\nEnter 'quit' if you're the last guest. "

while True:
    name = input(prompt)
    if name == 'quit':
        break
    else:
        guest_names.append(name)

# 创建一个字符串
file_string = ''
for guest_name in guest_names:
    file_string += f'{guest_name}\n'

path.write_text(file_string)